package com.mes.biz.service.impl;

import java.util.List;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.framework.cache.UserUtils;
import com.mes.common.core.domain.entity.SysUser;
import com.mes.common.utils.StringUtils;
import com.mes.biz.mapper.BizWarehouseMapper;
import com.mes.biz.domain.BizWarehouse;
import com.mes.biz.service.BizWarehouseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 仓库Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-04-01
 */
@Service
@Transactional(readOnly = true)
public class BizWarehouseServiceImpl extends BaseServiceImpl<BizWarehouseMapper, BizWarehouse> implements BizWarehouseService {

    private static final Logger log = LoggerFactory.getLogger(BizWarehouseServiceImpl.class);

    /**
     * 获取单条数据
     * @param bizWarehouse 仓库
     * @return 仓库
     */
    @Override
    public BizWarehouse get(BizWarehouse bizWarehouse) {
        BizWarehouse dto = super.get(bizWarehouse);
        if (StringUtils.isNotEmpty(dto.getChargePerson())){
            SysUser sysUser = UserUtils.getSysUser(dto.getChargePerson());
			if (null != sysUser){
				dto.setChargePersonName(sysUser.getName());
			}
        }
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 仓库id
     * @return 仓库
     */
    @Override
    public BizWarehouse get(String id) {
        BizWarehouse dto = super.get(id);
        if (StringUtils.isNotEmpty(dto.getChargePerson())){
			SysUser sysUser = UserUtils.getSysUser(dto.getChargePerson());
			if (null != sysUser){
				dto.setChargePersonName(sysUser.getName());
			}
		}
        return dto;
    }

    /**
     * 查询仓库列表
     * @param bizWarehouse 仓库
     * @return 仓库
     */
    @Override
    public List<BizWarehouse> findList(BizWarehouse bizWarehouse) {
		List<BizWarehouse> bizWarehouseList = super.findList(bizWarehouse);
		if (!CollectionUtils.isEmpty(bizWarehouseList)) {
			bizWarehouseList.stream().forEach(dto -> {
				if (StringUtils.isNotEmpty(dto.getChargePerson())){
					SysUser sysUser = UserUtils.getSysUser(dto.getChargePerson());
					if (null != sysUser){
						dto.setChargePersonName(sysUser.getName());
					}
				}
			});
		}
        return bizWarehouseList;
    }

    /**
     * 分页查询仓库列表
     * @param bizWarehouse 仓库
     * @return 仓库
     */
    @Override
    public PageInfo<BizWarehouse> findPage(BizWarehouse bizWarehouse) {
		PageInfo<BizWarehouse> page = super.findPage(bizWarehouse);
		if (!CollectionUtils.isEmpty(page.getList())) {
            page.getList().stream().forEach(dto -> {
				if (StringUtils.isNotEmpty(dto.getChargePerson())){
					SysUser sysUser = UserUtils.getSysUser(dto.getChargePerson());
					if (null != sysUser){
						dto.setChargePersonName(sysUser.getName());
					}
				}
            });
        }
        return page;
    }

    /**
     * 保存仓库
     * @param bizWarehouse
     * @return 结果
     */
    @Override
    public boolean save(BizWarehouse bizWarehouse) {
        return super.save(bizWarehouse);
    }

    /**
     * 删除仓库信息
     * @param bizWarehouse
     * @return 结果
     */
    @Override
    public boolean remove(BizWarehouse bizWarehouse) {
        return super.remove(bizWarehouse);
    }

    /**
     * 批量删除仓库
     * @param ids 需要删除的仓库ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBizWarehouseByIds(String[] ids) {
        return mapper.deleteBizWarehouseByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Override
    public List<BizWarehouse> allListByProductId(String productId) {
        return mapper.allListByProductId(productId);
    }

}
